import 'package:flutter/material.dart';
import '../data/CustomData.dart';

// http://m.maoyan.com/asgard/asgardapi/media/activity/ad/movie/detail.json?movieId=1277939

// 请求数据
import 'package:dio/dio.dart';
import 'dart:convert';

class Detail extends StatefulWidget {
  Detail({
    Key key,
    @required this.id,
    @required this.nm,
  }) : super(key: key);
  final int id;
  final String nm;
  @override
  _DetailState createState() => _DetailState();
}

class _DetailState extends State<Detail> {
  final Map detailMap = new CustomData().detailMap;
  Map map = {};
  bool flag = false;
  getDetail()async{
    try {
      Dio dio = new Dio();
      Response response = await dio.get('http://m.maoyan.com/ajax/detailmovie?movieId=${widget.id}');
      Map responseData = json.decode(response.toString());
      setState(() {
       map = responseData["detailMovie"];
       flag = true;
      });
      
    } catch (e) {
      print(e);
    }
  }
  @override
  void initState() {
    getDetail();
    super.initState();
  }
  String _imgReset(String img) => img.replaceAll('/w.h/', '/');
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text('${widget.nm}'),
          centerTitle: true,
        ),
        body: ListView(
          children: <Widget>[
            Container(
              margin: EdgeInsets.only(top: 40, bottom: 30),
              child: Image(
                fit: BoxFit.contain,
                height: 315,
                image: flag == false ? AssetImage('images/loading.png') : NetworkImage('${_imgReset(map["img"])}'),
              ),
            ),
            _TextItem(
              title: '名称',
              text: map["nm"],
            ),
            _TextItem(
              title: '年代',
              text: map["rt"],
            ),
            _TextItem(
              title: '产地',
              text: map["src"],
            ),
            _TextItem(
              title: '类别',
              text: map["cat"],
            ),
            _TextItem(
              title: '语言',
              text: map["oriLang"],
            ),
            _TextItem(
              title: '上映时间',
              text: map["pubDesc"],
            ),
            _TextItem(
              title: '豆瓣评分',
              text: map["sc"].toString(),
            ),
            _TextItem(
              title: '片长',
              text: map["dur"].toString(),
            ),
            _TextItem(
              title: '导演',
              text: map["dir"],
            ),
            _TextItem(
              title: '主演',
              text: map["star"],
              axisFlag: true,
            ),
            _TextItem(
              title: '简介',
              text: map["dra"],
              axisFlag: true,
            ),
            Padding(
              padding: EdgeInsets.only(bottom: 30),
            )
          ],
        ));
  }
}

class _TextItem extends StatelessWidget {
  _TextItem({
    Key key,
    @required this.title,
    @required this.text,
    this.axisFlag,
  }) : super(key: key);
  final String title;
  final String text;
  final bool axisFlag;
  
  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.fromLTRB(10, 0, 10, 5),
      child: Row(
        crossAxisAlignment: axisFlag == true ? CrossAxisAlignment.start : CrossAxisAlignment.center,
        children: <Widget>[
          Container(
            width: 85,
            child: title.length == 2 ? Text('◎ ${title[0]}　　${title[1]}') : Text('◎ $title'),
          ),
          Expanded(
            child: Container(
              child: Text('$text'),
            ),
          ),
        ],
      ),
    );
  }
}
